<?php 

class FRM_Filter_Post
{
	/**
	 * default method, strips tags
	 *
	 * @param string $key
	 */
	static function get($key)
	{
		$filter = new Zend_Filter_StripTags();
		$post = self::toObject();
		return trim($filter->filter($post->$key));

	}

	/**
	 * set a post value
	 *
	 * @param string $key
	 * @param string $value
	 */
	static function set($key, $value)
	{
		$_POST[$key] = $value;
	}

	/**
	 * returns the whole post array as an object
	 *
	 * @return stdClass object
	 */
	static function toObject()
	{
		$post = new stdClass();
		foreach ($_POST as $k => $v)
		{
			$post->$k = stripslashes($v);
		}
		return $post;
	}

	/**
	 * test whether the key is set
	 *
	 * @param string $key
	 * @return bool
	 */
	static function has($key, $post=null){
		if(is_null($post)){
			$post = $_POST;
		}
		
		return isset($post[$key]) || isset($_SESSION['admin_images'][$key]);
	}

	/**
	 * returns the value without any filters
	 *
	 * @param string $key
	 * @return mixed
	 */
	static function raw($key, $post=null)
	{
		if(is_null($post)){
			$post = $_POST;
		}
		//print_r($_SESSION);
		if(isset($_SESSION['admin_images'][$key])){
			$r= $_SESSION['admin_images'][$key];
			
			unset($_SESSION['admin_images'][$key]);
			return $r;
		}
		
		return $post[$key];
	}

	/**
	 * filters the value as alpha
	 *
	 * @param string $key
	 * @return string
	 */
	static function alpha($key)
	{
		$filter = new Zend_Filter_Alpha();
		$post =self::toObject();
		return trim($filter->filter($post->$key));
	}

	/**
	 * filters the value as an integer
	 *
	 * @param string $key
	 * @return int
	 */
	static function int($key)
	{
		$post = self::toObject();
		return intval($post->$key);
	}

	/**
	 * strips and adds slashes. i strip them first because the html editor adds them as well
	 *
	 * @param string $key
	 * @return string
	 */
	static function text($key)
	{
		//you must strip slashes first, as the HTML editors add them
		//by doing this you are able to process both raw HTML and WYSIWYG HTML
		$post = self::toObject();
		return trim(addslashes($post->$key));
	}

	/**
     * returns the value as a floating point #
     *
     * @param string $key
     * @return float
     */
	static function float($key)
	{
		$post = self::toObject();
		if(is_float($post[$key])){
			return floatval($post->$key);
		}
	}

	/**
     * returns the value as a timestamp
     * the value is evaluated with zend_date
     *
     * @param string $key
     * @return timestamp
     */
	static function date($key)
	{
		$post =self::toObject();
		if($post[$key]){
			$date = new Zend_Date($post->$key);
			return $date->get(Zend_Date::TIMESTAMP);
		}
	}

	/**
     * dumps the post variables
     *
     */
	static function dump()
	{
		Zend_Debug::dump($_POST);
	}

}